package com.mokee.android.ShipPass;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import com.mokee.Util.ShipAttrUtil;
import com.mokee.android.database.DataBaseHelper;
import com.mokee.android.database.UserTable;

/**
 * 在此类中显示每一项的列表
 * @author Mokee_Work
 */
public class ShipProcessListActivity extends Activity implements OnClickListener {
	private static final String tag = "ShipProcessListActivity";
	
	private ListView lv_ShipListItem;
	private Button btn_TopReturn;
	private TextView layout_sub_Title, iv_Submit;
	
	private SimpleAdapter adapter;
	private List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
	
	private int process;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_ship_list_item);
		
		Bundle data = getIntent().getBundleExtra("process_bundle");
		process = data.getInt("process");
		
		initView();
	}

	private void initView() {
		lv_ShipListItem = (ListView) findViewById(R.id.lv_ShipListItem);
		btn_TopReturn = (Button) findViewById(R.id.btn_TopReturn);
		layout_sub_Title = (TextView) findViewById(R.id.layout_sub_Title);
		iv_Submit = (TextView) findViewById(R.id.iv_Submit);
		
		iv_Submit.setVisibility(View.GONE);
		layout_sub_Title.setText(ShipAttrUtil.shipProcess(process));
		
		btn_TopReturn.setOnClickListener(this);
		
		lv_ShipListItem.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				Intent detailIntent = new Intent(ShipProcessListActivity.this, ShipProcessItemDetailActivity.class);
				Bundle data = new Bundle();
				data.putInt("id", (Integer) list.get(position).get("id"));
				detailIntent.putExtra("bundle", data);
				startActivity(detailIntent);
			}
		});
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		
		lv_ShipListItem.setEmptyView(findViewById(R.id.empty));
		loadingData();
	}

	/**
	 * 加载数据到ListView
	 */
	private void loadingData() {
		/** 从数据库查询数据   */
		setListData();
		
		// 设置适配器
		Context context = getApplicationContext();
		int resource;
		String[] from;
		int[] to;
		if(process == 0){
			resource = R.layout.ship_process_listview;
			from = new String[]{ "ship_name", "safe_level", "id", 
								 "all_weight", "goods_weight", "load_type", 
								 "orientation", "sail_start", "sail_end" };
			to = new int[]{ R.id.tv_LvShipName, R.id.tv_LvShipSafeLevel, R.id.tv_LvShipProcessID,
					 	    R.id.tv_LvShipAllWeight, R.id.tv_LvShipGoodsWeight, R.id.tv_LvShipLoadType,
					 	    R.id.tv_LvShipOrientation, R.id.tv_LvShipSailStart, R.id.tv_LvShipSailEnd };
		} else {
			resource = R.layout.ship_process_listview_money;
			from = new String[]{ "ship_name", "safe_level", "id", "all_weight", "goods_weight", 
					  			 "load_type", "orientation", "sail_start", "sail_end", "pay_type",
					  			 "pay_money", "ticket_number"};
			to = new int[]{ R.id.tv_LvShipName, R.id.tv_LvShipSafeLevel, R.id.tv_LvShipProcessID,
			 	    R.id.tv_LvShipAllWeight, R.id.tv_LvShipGoodsWeight, R.id.tv_LvShipLoadType,
			 	    R.id.tv_LvShipOrientation, R.id.tv_LvShipSailStart, R.id.tv_LvShipSailEnd,
			 	    R.id.tv_LvPayType, R.id.tv_LvPayMoney, R.id.tv_LvTicketNumber};
		}
		
		adapter = new SimpleAdapter(context, list, resource, from, to);
//		adapter = new MySimpleAdapter(context, list, resource, from, to);
//		if(list.size() <= 0){
//			// Toast.makeText(this, "无数据", Toast.LENGTH_SHORT).show();
//		} else {
//			lv_ShipListItem.setAdapter(adapter);
//		}
		lv_ShipListItem.setAdapter(adapter);
	}
	
	/**
	 * 从数据库查询数据
	 */
	private void setListData() {
		
		if(list.size() > 0){
			Log.i(tag, "list:" + list.size());
			list.clear();
		}
		
		List<UserTable> shipList = DataBaseHelper.getInstance().getUserRecord().queryForProcess(process);
		
		if(shipList != null){
			for(UserTable item : shipList){
				Map<String, Object> map = new HashMap<String, Object>();
				
				map.put("ship_name", item.getShipName());
				map.put("safe_level", ShipAttrUtil.shipSafeLevel(item.getShipSafeLevel()));
				map.put("id", item.getId());
				map.put("all_weight", item.getShipWeightNow());
				map.put("goods_weight", item.getShipWeightNow() - item.getShipWeightOri());
				map.put("load_type", ShipAttrUtil.shipLoadType(item.getShipLoadType()));
				map.put("orientation", ShipAttrUtil.shipOrientation(item.getShipSailOrientation()));
				map.put("sail_start", item.getShipSailStart());
				map.put("sail_end", item.getShipSailEnd());
				if(process != 0){
					map.put("pay_type", item.getShipPayType());
					map.put("pay_money", item.getShipPayMoney());
					String ticketNumber = item.getShipTicketNumber();
					if(ticketNumber == null || ticketNumber.equals("")){
						ticketNumber = "暂无票号";
					}
					map.put("ticket_number", ticketNumber);
				}
				
				list.add(map);
			}
		}
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_TopReturn:
			finish();
			onDestroy();
			break;

		default:
			break;
		}
	}
}
